package com.fanbuchi.fukua.fragment;

import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.fanbuchi.fukua.R;
import com.fanbuchi.fukua.activity.base.BaseFragment;
import com.fanbuchi.fukua.adapter.MeCenterAdapter;
import com.fanbuchi.fukua.api.ProfileAPI;
import com.fanbuchi.fukua.entity.OauthEntity;
import com.fanbuchi.fukua.entity.UserEntity;
import com.fanbuchi.fukua.manager.OauthManager;
import com.fanbuchi.fukua.manager.UserManager;
import com.fanbuchi.fukua.net.BaseCallBack;
import com.squareup.okhttp.Callback;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2015/7/16.
 */
public class MeCenterFragment extends BaseFragment {

    @InjectView(R.id.head)
    SimpleDraweeView head;
    @InjectView(R.id.nickName)
    TextView nickName;
    @InjectView(R.id.slogon)
    TextView slogon;
    @InjectView(R.id.mecenter)
    GridView mecenter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView=inflater.inflate(R.layout.fragment_mecenter,container,false);
        ButterKnife.inject(this, rootView);

        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mecenter.setAdapter(new MeCenterAdapter(getActivity(), null));
        setProfile();
        getProfile();
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }
    @Override
    public void onResume() {
        super.onResume();

    }

    protected void getProfile(){
        OauthEntity oauth= OauthManager.getOauthEntity();
        if (oauth!=null){
            SparseArray<String> _result= ProfileAPI.getAPI().profile(profileCallBack());
            if (_result!=null){
                setProfile();
            }
        }
    }


    public Callback profileCallBack(){
        Callback _callback=  new BaseCallBack<UserEntity>(UserEntity.class) {
            @Override
            public void onResponseOk(UserEntity body) {
                if (body.code==0){
                    UserManager.setUserEntity(body);
                }else{
                    OauthManager.setOauthEntity(null);
                }

            }
        };
        return _callback;
    }

    protected void setProfile(){
        UserEntity userEntity= UserManager.getUserEntity();
        if (userEntity!=null){
            nickName.setText(userEntity.getNickName());
            slogon.setText(userEntity.getSlogon());
            Uri uri = Uri.parse(userEntity.getIdenticon());
            head.setImageURI(uri);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ButterKnife.reset(this);
    }
}
